/*
 * Copyright (c) 2011 Junichiro Okuno
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Junichiro Okuno.
 */

package tcp_net.management;

import tcp_net.TextInputOutput;
import tcp_net.management.debug.DebugInfo;
import tcp_net.management.user_account.UserAccountManager;
import tcp_net.Server;
import tcp_net.mail.pop3.POP3Server;
import tcp_net.mail.smtp.SMTPServer;

/**
	This class provides to manage POP3 and SMTP servers
	@author J. Okuno
*/
public class EmailServerManager
{
	/**
		A file path to POP3 server port number
	*/
	private final static String POP3_PORT_PATH = "conf/pop3_port.txt";
	
	/**
		A file path to SMTP server port number
	*/
	private final static String SMTP_PORT_PATH = "conf/smtp_port.txt";
	
	/**
		Debug information
	*/
	private final static boolean DEBUG = DebugInfo.EMAIL_SERVER_MANAGER_DEBUG;
	
	/**
		A POP3 server
	*/
	private POP3Server pop3;
	
	/**
		A SMTP server
	*/
	private SMTPServer smtp;
	
	/**
		A user account manager
	*/
	private UserAccountManager ua_manager;
	
	/**
		A backup file manager
	*/
	private BackupFileManager backup_manager;
	
	/**
		A number of POP3 server handlrs to be started
	*/
	private int no_of_pop3;
	
	/**
		A number of SMTP server handlrs to be started
	*/
	private int no_of_smtp;
	
	/**
		Init an email server manager
		@param n_pop3 a number of POP3 server connections to be avaliable running
		@param n_smtp a number of SMTP server connections to be avaliable running
	*/
	public EmailServerManager(int n_pop3, int n_smtp) 
	{ 
		no_of_pop3 = n_pop3;
		no_of_smtp = n_smtp;
		
		ua_manager = new UserAccountManager();
		backup_manager = new BackupFileManager(this);
		
		DebugInfo.print(DEBUG, "EmailServerManager created");
	}
	
	/**
		This method returns a user account manager object
		@return a UserAccountManager object
	*/
	public UserAccountManager getUserAccountManager() 
	{ 
		DebugInfo.print(DEBUG, "UserAccountManager is requested from EmailServerManager");
		return ua_manager; 
	}
	
	/**
		This method returns a backup file manager object
		@return a BackupFileManager object
	*/
	public BackupFileManager getBackupFileManager() 
	{ 
		DebugInfo.print(DEBUG, "BackupFileManager is requested from EmailServerManager");
		return backup_manager; 
	}
	
	/**
		This method returns a POP3 server object
		@return a POP3 server object
	*/
	public Server getPOP3Server() { return pop3; }
	
	/**
		This method returns a SMTP server object
		@return an SMTP server object
	*/
	public Server getSMTPServer() { return smtp; }
	
	/**
		This method starts both SMTP and POP3 servers
	*/
	public void start()
	{
		int pop3_port = 0;
		int smtp_port = 0; 
		
		try
		{
			 pop3_port = Integer.parseInt(TextInputOutput.getText(POP3_PORT_PATH));
			 smtp_port = Integer.parseInt(TextInputOutput.getText(SMTP_PORT_PATH));
		}
		catch(Exception ex) 
		{ 
			//Default ports
			pop3_port = 110;
			smtp_port = 25; 
		}
		
		//POP3 and SMTP ports must not be the same
		if(pop3_port != smtp_port && pop3==null && smtp==null)
		{	
			pop3 = new POP3Server(pop3_port, no_of_pop3, this);
			smtp = new SMTPServer(smtp_port, no_of_smtp, this);
		
			pop3.startServer();
			smtp.startServer();
		
			//backup_manager.restore();
			DebugInfo.print(DEBUG,"SMTP and POP3 servers started");
		}
		
		else
		{
			DebugInfo.print(DEBUG,"SMTP and POP3 servers NOT started. POP3 and SMTP ports must not be the same");
		}
	}
	
	/**
		This method shutdowns both SMTP and POP3 server
	*/
	public void shutdown()
	{
		if(pop3!=null && smtp!=null)
		{
			pop3.endServer();
			smtp.endServer();
		
			pop3 = null;
			smtp = null;
		
			//backup_manager.save();
			DebugInfo.print(DEBUG,"SMTP and POP3 servers shutdowned");
		}
	}
	
	/**
		This method restarts both SMTP and POP3 servers
	*/
	public void restart()
	{
		if(pop3!=null && smtp!=null)
		{
			shutdown();
			start();
		
			DebugInfo.print(DEBUG,"SMTP and POP3 servers restarted");
		}
	}
}